function wait(time) {
  return new Promise((resolve, reject) => {
    try {
      setTimeout(() => {
        resolve();
      }, time);
    } catch (error) {
      reject(error);
    }
  });
}

export default {
  namespace: "compute1",
  state: {
    count: 0,
    zoom: 1
  },
  reducers: {
    add(state) {
      const { count } = state;

      return Object.assign({}, state, { count: count + 1 });
    },
    minus(state) {
      const { count } = state;

      return Object.assign({}, state, { count: count - 1 });
    },
    changeSize(state, zoom) {
      return Object.assign({}, state, {
        zoom
      });
    }
  },
  flows: {
    asyncAdd: async (actions, payload, getState) => {
      await wait(payload || 1000);
      actions["compute1"].add();

      const { count } = getState().compute1;

      actions["compute1"].changeSize(count);
    },
    asyncMinus: async (actions, payload, getState) => {
      await wait(payload || 500);

      actions["compute1"].minus();

      const { count } = getState().compute1;

      actions["compute1"].changeSize(count);
    }
  }
};
